<template>
  <div class="app-container">
    <div class="detailsbody">
      <div class="top">商品发布</div>
      <ul class="tiltes">
        <li class="active" v-if="commodity_details">基本信息</li>
        <li class="active" v-if="commodityd_escription">商品描述</li>
      </ul>
      <el-card class="detail_info" style="display:flex" v-if="commodity_details">
        <el-form :model="value" ref="productInfoForm" label-width="120px" size="small" class="el-form-table">
          <div class="detail_info-item">
            <div style="display:flex;margin-bottom:24px;">
              <span class="tilte" style="width:102px;">商品信息</span>
              <div class="line"></div>
            </div>
            <el-form-item label="商品名称" required prop="name">
              <el-input v-model="brand.name"></el-input>
            </el-form-item>
            <el-form-item label="商品图片" required prop="subTitle">
              <div class="tips">建议尺寸：750*450像素，最多1张</div>
              <div class="wrapper">
                <div class="model" v-show="model" @click="model = true">
                  <div class="model-show">
                    <img :src="modelSrc" alt="">
                  </div>
                </div>
                <div class="content" v-show="showinfo">
                  <div class="show-info">
                    <div class="test">
                      <vueCropper ref="cropper2" :img="example2.img " :outputSize="example2.size" :outputType="example2.outputType" :info="example2.info" :canScale="example2.canScale" :autoCrop="example2.autoCrop" :autoCropWidth="example2.autoCropWidth" :autoCropHeight="example2.autoCropHeight" :fixed="example2.fixed" :fixedNumber="example2.fixedNumber" :enlarge="4"></vueCropper>
                    </div>
                    <label class="btn" for="upload2">上传</label>
                    <input type="file" id="upload2" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event,2)">
                    <button @click="finish2()" class="btn">裁剪</button>
                  </div>
                </div>
              </div>
              <!-- <single-upload v-model="brand.pic"></single-upload> -->
            </el-form-item>
            <el-form-item label="商品展示图" required>
              <div class="tips">建议尺寸：100*100像素，最多1张</div>
              <div class="showcaseimg">
                <div class="model" v-show="commoditymodel" @click="commoditymodel = true">
                  <div class="model-show">
                    <img :src="modelSrc" alt="">
                  </div>
                </div>
                <div class="contentimg" v-show="showinfo">
                  <div class="show-info">
                    <div class="test">
                      <vueCropper ref="cropper2" :img="example2.img " :outputSize="example2.size" :outputType="example2.outputType" :info="example2.info" :canScale="example2.canScale" :autoCrop="example2.autoCrop" :autoCropWidth="example2.autoCropWidth" :autoCropHeight="example2.autoCropHeight" :fixed="example2.fixed" :fixedNumber="example2.fixedNumber" :enlarge="4"></vueCropper>
                    </div>
                    <label class="btn" for="upload2">上传</label>
                    <input type="file" id="upload2" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event,2)">
                    <button @click="finish2()" class="btn">裁剪</button>
                  </div>
                </div>
                <!-- <single-upload v-model="brand.picSquare"></single-upload> -->
              </div>
            </el-form-item>
            <el-form-item label="商品价格" required>
              <el-input v-model="brand.price" placeholder="请输入内容"></el-input>
              <el-checkbox v-model="checked">促销活动</el-checkbox>
            </el-form-item>
            <el-form-item label="商品库存" required>
              <el-input v-model="brand.stock"></el-input>
            </el-form-item>
            <el-form-item label="数量规格" required>
              <el-input v-model="brand.amountSpecification" aria-placeholder="如：320ml/32颗装等"></el-input>
            </el-form-item>
            <el-form-item label="包装规格" required>
              <el-input v-model="brand.packSpecification" aria-placeholder="如：盒/件/瓶/袋等"></el-input>
            </el-form-item>
            <el-form-item label="成本价格" required>
              <el-input v-model="brand.costPrice"></el-input>填写成本可以帮助您统计利润数据
            </el-form-item>
          </div>
          <div class="detail_info-item">
            <div style="display:flex">
              <span class="tilte" style="width:102px;">物流信息</span>
              <div class="line"></div>
            </div>
            <el-form>
              <div class="details_set">
                <p>
                  <em>*</em>运费设置
                </p>
                <el-form-item>
                  <!-- 邮费类型：0统一邮费；1全国包邮；2本省包邮,外地统一邮费 -->
                  <el-radio-group v-model="brand.postageType">
                    <el-radio :label="0">统一邮费
                      <el-input v-model="brand.postage" style="width: 100px!important;height: 30px!important;"></el-input>
                    </el-radio>
                    <el-radio :label="1">全国包邮</el-radio>
                    <el-radio :label="2">本省包邮，外地统一邮费
                      <!-- <el-input v-model="brand.postage" style="width: 100px!important;height:30px!important;"></el-input> -->
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </div>
            </el-form>
          </div>
          <div class="detail_info-item">
            <div style="display:flex">
              <span class="tilte" style="width:102px;">销售说明</span>
              <div class="line"></div>
            </div>
            <el-form style="display:flex">
              <div class="details_set">
                <p>
                  <em>*</em>是否可退换
                </p>
                <div class="Replacement">
                  <!--  -->
                  <el-form-item>
                    <el-radio-group v-model="brand.isExchange">
                      <el-radio :label="0">可退换</el-radio>
                      <el-radio :label="1">不可退换</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </div>
              <div class="details_set">
                <p>
                  <em>*</em>积分使用
                </p>
                <div class="Integralusage">
                  <!--  -->
                  <el-form-item>
                    <el-radio-group v-model="brand.isUseIntegral">
                      <el-radio :label="0">不可使用积分</el-radio>
                      <el-radio :label="1">可使用积分（一积分兑换一元）</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </div>
            </el-form>
          </div>
          <el-form-item style="text-align: center">
            <el-button type="primary" @click="headleNextstep()">下一步</el-button>
            <el-button @click="cancel('brandFrom')">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <div v-if="commodityd_escription" style="height:610px;">
        <div class="" style="height: 500px;border:none;">
          <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" style="height: 450px;border:none;"></quill-editor>
        </div>
        <el-form>
          <el-form-item style="text-align: center">
            <el-button type="primary" size="medium" @click="handlepreview()">预览</el-button>
            <el-button type="primary" size="medium" @click="handleSave()">上架</el-button>
            <el-button @click="handleCancel()">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";
import codes from "@/components/Upload/code";
import { quillEditor } from "vue-quill-editor";
import { createBrand, Commoditydetails, Fileupload } from "@/api/product";
import SingleUpload from "@/components/Upload/singleUpload";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
const defaultBrand = {
  name: "",
  picSquare: "",
  pic: "",
  price: "",
  stock: "",
  brandStory: "",
  amountSpecification: "",
  packSpecification: "",
  postageType: 0,
  isExchange: 0,
  isUseIntegral: 0,
  firstLetter: "",
  publishStatus: 0,
  description: "",
  descriptionPic: ""
};
export default {
  name: "FuncFormsEdit",
  components: { SingleUpload, quillEditor, VueCropper, codes },
  data() {
    return {
      brand: Object.assign({}, defaultBrand),
      navIndex: 1, // 导航
      commodity_details: true,
      commodityd_escription: false,
      content: null,
      showinfo: true,
      BASE_API: "http://47.105.156.237:8083",
      editorOption: {
        placeholder: "请输入"
        // theme: "bubble", // or 'bubble'
        // modules: {
        //   toolbar: {
        //     container: "#toolbar"
        //   }
        // }
      },
      model: false,
      commoditymodel:false,
      modelSrc: "",
      crap: false,
      previews: {},
      filedetails: null,
      form: {
        head: ""
      },
      example2: {
        //img的路径自行修改
        img: "",
        info: true,
        size: 1,
        outputType: "jpeg",
        canScale: true,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 300,
        autoCropHeight: 250,
        fixed: true,
        // 真实的输出宽高
        infoTrue: true,
        fixedNumber: [4, 3]
      },
      downImg: "#",
      dialogVisible: false,
      disabled: false,
      imageUrl: "", //图像地址
      checked: false, //是否促销
      radio: "1",
      Replacement: "1", //是不是可退换的
      Integral: "1", //积分使用
      navList: [
        {
          id: 1,
          txt: "基本信息",
          status: "1"
        }
        // {
        //   id: 2,
        //   txt: "商品描述",
        //   status: "2"
        // }
      ],
      value: {
        name: "九转黄精丸【美肤排毒】"
      }
    };
  },
  methods: {
    switchTab(item) {
      this.navIndex = item.id;
      if (this.navIndex == 1) {
        this.commodity_details = true;
        this.commodityd_escription = false;
      } else {
        this.commodity_details = false;
        this.commodityd_escription = true;
      }
    },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
    onSubmit() {
      createBrand(this.brand).then(response => {
        this.$refs[formName].resetFields();
        this.brand = Object.assign({}, defaultBrand);
        this.$message({
          message: "提交成功",
          type: "success",
          duration: 1000
        });
      });
    },
    headleNextstep() {
      this.commodity_details = false;
      this.commodityd_escription = true;
    },
    // 预览
    handlepreview() {},
    // 上架
    handleUp() {},
    //取消
    handleCancel() {},
    //点击裁剪，这一步是可以拿到处理后的地址
    finish2() {
      // console.log(this.file, 2222222);

      this.$refs.cropper2.getCropData(data => {
        // this.modelSrc = data;
        // this.model = false;
        // //裁剪后的图片显示
        // this.example2.img = this.modelSrc;

        let filedetails = this.dataURLtoFile(data, "jpg");
        let fileFormData = new FormData();
        fileFormData.append("file", filedetails);
        var BASE_API = "http://47.105.156.237:8083";
        Fileupload(fileFormData).then(Response => {
          this.modelSrc = BASE_API + Response.data;
          this.model = true;
          this.showinfo = false;
          //裁剪后的图片显示
          this.example2.img = "222222222";
        });
        this.toBlob(data);
        console.log(data);
        console.log(this.toBlob(data), 2222222);

        //阿里云处理图片，项目的接口，这里可以不用，上面的地址打印即为base64的地址
        // this.$api.admin.url(data => {
        //   new OSS.Wrapper({
        //     region: "oss-cn-hangzhou",
        //     accessKeyId: data.accessKeyId,
        //     accessKeySecret: data.accessKeySecret,
        //     stsToken: data.securityToken,
        //     // bucket: 'mybg'c
        //     bucket: "zhiyuan-hz"
        //   })
        //     .put(data.key, this.toBlob(this.example2.img))
        //     .then(data => {
        //       console.log(data.url);
        //     })
        //     .catch(function(err) {
        //       console.error("error: %j", err);
        //     });
        // });
      });
    },

    dataURLtoFile(dataurl, filename) {
      //将base64转换为文件
      var arr = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], filename, { type: mime });
    },

    uploadImg(e, num) {
      //上传图片
      this.example2.img = "";
      var file = e.target.files[0];
      this.filedetails = e.target.files[0];
      var fileName = e.target.files[0].name;
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isLt2M) {
        this.$message.warning("上传模板大小不能超过 5MB!");
        return;
      }
      if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
        alert("图片类型必须是.gif,jpeg,jpg,png,bmp中的一种");
        return false;
      }

      var reader = new FileReader();
      reader.onload = e => {
        let data;
        data = e.target.result;
        if (typeof e.target.result === "object") {
          // 把Array Buffer转化为blob 如果是base64不需要
          //  var URL='http://47.105.156.237:8083'
          data = window.URL.createObjectURL(new Blob([e.target.result]));
          console.log(data, 222222222);
        } else {
          data = e.target.result;
        }
        if (num === 1) {
          this.option.img = data;
        } else if (num === 2) {
          this.example2.img = data;
        }
      };
      // 转化为base64
      // reader.readAsDataURL(file)
      // 转化为blobcs
      reader.readAsArrayBuffer(file);
    },
    // base64转blob
    toBlob(ndata) {
      //ndata为base64格式地址
      let arr = ndata.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], {
        type: mime
      });
    }
    // getUEContent() {
    //   // 获取内容方法
    //   return this.editor.getContent();
    // },
    // destroyed() {
    //   this.editor.destroy();
    // }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
  background: #f1f1f1;
  min-height: 500px;
  .detailsbody {
    background: #fff;
    width: 100%;
    height: 100%;
    .tiltes {
      display: flex;
      font-size: 16px;
      font-family: AlibabaPuHuiTiR;
      border-bottom: 1px solid #cccccc;
      background: transparent;
      padding-left: 20px !important;
      line-height: 48px !important;
      box-sizing: border-box;
      padding: 0 !important;
      margin: 0 !important;
      li {
        width: 136px;
        text-align: center;
      }
      li.active {
        width: 136px;
        height: 4px;
        height: 48px;
        line-height: 48px !important;
        border-bottom: 4px solid rgba(96, 118, 255, 1);
        box-sizing: border-box;
      }
    }
    .el-card {
      box-shadow: none !important;
      border: none;
      .el-card__body {
        .detail_info-item {
          .flex {
            display: flex;
          }
          .title {
            font-size: 16px;
            width: 102px;
            font-family: AlibabaPuHuiTiM;
            color: rgba(0, 0, 0, 1);
          }
          .line {
            width: 1000px;
            padding-top: 12px;
            border-bottom: 2px solid #f1f1f1;
            // background: #f1f1f1;
          }
          .details_set {
            padding: 20px 0px;
            box-sizing: border-box;
            margin-left: 100px;
            .postage {
              input.el-input__inner {
                width: 100px !important;
                height: 30px !important;
              }
            }
            p {
              em {
                color: #ff0000;
                margin-right: 4px;
              }
            }
            span {
              margin-left: 4px;

              em {
                margin-right: 4px;
                font-style: normal;
              }
              b {
                color: #ccc;
              }
            }
          }
          .el-form-table {
            padding-top: 20px;
            margin-left: 80px;
            .tips {
              color: #999;
              font-size: 12px;
            }
            .showcaseimg{
              width: 100px;
            }
          }
        }
      }
    }
  }
  .top {
    padding: 4px 25px 0;
    line-height: 34px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #f1f1f1;
    box-sizing: border-box;
    .edit {
      background: #169bd5;
      color: #fff;
      width: 73px;
      height: 32px;
      line-height: 32px;
      text-align: center;
      border-radius: 4px;
    }
  }
  .commodity_escription {
    padding: 30px;
    box-sizing: border-box;
    .description {
      height: 300px;
      margin-bottom: 10px;
    }
  }
}
</style>

<style>
.content {
  max-width: 750px;
}
.test-button {
  display: flex;
  flex-wrap: wrap;
}

.btn {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #fff;
  border: 1px solid #c0ccda;
  color: #1f2d3d;
  text-align: center;
  box-sizing: border-box;
  outline: none;
  margin: 20px 10px 0px 0px;
  padding: 9px 15px;
  font-size: 14px;
  border-radius: 4px;
  color: #fff;
  background-color: #50bfff;
  border-color: #50bfff;
  transition: all 0.2s ease;
  text-decoration: none;
  user-select: none;
}

.des {
  line-height: 30px;
}

code.language-html {
  padding: 10px 20px;
  margin: 10px 0px;
  display: block;
  background-color: #333;
  color: #fff;
  overflow-x: auto;
  font-family: Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo,
    Lucida, Sans, Type, Writer, Ubuntu, Mono;
  border-radius: 5px;
  white-space: pre;
}

.show-info h2 {
  line-height: 50px;
}

/*.title, .title:hover, .title-focus, .title:visited {
        color: black;
    }*/

.title {
  display: block;
  text-decoration: none;
  text-align: center;
  line-height: 1.5;
  margin: 20px 0px;
  background-image: -webkit-linear-gradient(
    left,
    #3498db,
    #f47920 10%,
    #d71345 20%,
    #f7acbc 30%,
    #ffd400 40%,
    #3498db 50%,
    #f47920 60%,
    #d71345 70%,
    #f7acbc 80%,
    #ffd400 90%,
    #3498db
  );
  color: transparent;
  -webkit-background-clip: text;
  background-size: 200% 100%;
  animation: slide 5s infinite linear;
  font-size: 40px;
}

.test {
  height: 285px;
}

.model {
  width: 750px;
  height: 450px;
  overflow: auto;
  top: 0;
  left: 0;
  /* background: rgba(0, 0, 0, 0.8); */
}

/* .model-show {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
} */

.model img {
  display: block;
  margin: auto;
  max-width: 100%;
  user-select: none;
  background-position: 0px 0px, 10px 10px;
  background-size: 20px 20px;
  background-image: linear-gradient(
      45deg,
      #eee 25%,
      transparent 25%,
      transparent 75%,
      #eee 75%,
      #eee 100%
    ),
    linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
}

.c-item {
  display: block;
  padding: 10px 0;
  user-select: none;
}

@keyframes slide {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: -100% 0;
  }
}

@media screen and (max-width: 1000px) {
  .content {
    max-width: 90%;
    margin: auto;
  }

  .test {
    height: 400px;
  }
}
</style>

